using System;
using System.Data;
using System.Xml;

namespace FreeFlow.Administration
{
	/// <summary>
	/// A form in a procedure.
	/// </summary>
  public class Form : DatabaseAware
  {
    private string procedureName;
    internal Form(Server server, string procedureName, IDataReader reader) : base(server)
    {
      this.procedureName = procedureName;
      LoadFromReader(reader);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Form"/> class.
    /// </summary>
    /// <param name="server">The server.</param>
    /// <param name="procedureName">Name of the procedure.</param>
    /// <param name="formName">Name of the form.</param>
    public Form(Server server, string procedureName, string formName) : base(server)
    {
      if (string.IsNullOrEmpty(formName))
        throw new ArgumentNullException("formName", "formName must have a value");

      this.procedureName = procedureName;
      using (IDataReader reader = Server.ExecuteReader(
        string.Format("SELECT * FROM eForm WHERE eProcedureName = '{0}' AND eFormName = '{1}'", procedureName, formName)))
      {
        if (reader.Read()) 
          LoadFromReader(reader);
      }
    }

    private void LoadFromReader(IDataReader reader)
    {
      this.name = reader["eFormName"].ToString();
      string layout = reader["eLayout"].ToString();
      formLayout = new XmlDocument();
      formLayout.LoadXml(layout);
      whenUserLoadsForm = reader["eOnLoad"].ToString();
      whenUserSavesForm = reader["eOnSave"].ToString();
      restrictViewingTo = reader["eUserAccess"].ToString();
    }

    private string name;
    /// <summary>
    /// Gets the name of the form.
    /// </summary>
    public string Name
    {
      get
      {
        return name;
      }
    }

    private XmlDocument formLayout;
    /// <summary>
    /// Gets the form layout definition for this form.
    /// </summary>
    public XmlDocument FormLayout
    {
      get
      {
        return formLayout;
      }
    }

    private string whenUserLoadsForm;
    /// <summary>
    /// Gets the code evaluated when the user loads the form.
    /// </summary>
    public string WhenUserLoadsForm
    {
      get
      {
        return whenUserLoadsForm;
      }
    }

    private string whenUserSavesForm;
    /// <summary>
    /// Gets the code evaluated when the user saves the form.
    /// </summary>
    public string WhenUserSavesForm
    {
      get
      {
        return whenUserSavesForm;
      }
    }

    private FieldCollection fields;
    /// <summary>
    /// Gets the fields in this form.
    /// </summary>
    public FieldCollection Fields
    {
      get
      {
        if (fields == null)
          fields = new FieldCollection(Server, procedureName, name);
        return fields;
      }
    }

    private string restrictViewingTo;
    /// <summary>
    /// Gets the formula that will be valuated to decide which users have access to this form.
    /// </summary>
    public string RestrictViewingTo
    {
      get
      {
        return restrictViewingTo;
      }
    }
	}
}
